home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-13 | 7.0 KB | 299 lines | [TEXT/PJMM] |
- unit DemoFiles;
-
- { WASTE DEMO PROJECT: }
- { File Handling }
-
- { Copyright © 1993-1994 Merzwaren }
- { All Rights Reserved }
-
- interface
- uses
- DemoIntf;
-
- function ReadTextFile (pFileSpec: FSSpecPtr;
- hWE: WEHandle): OSErr;
- function WriteTextFile (pFileSpec: FSSpecPtr;
- hWE: WEHandle): OSErr;
- function TranslateDrag (theDrag: DragReference;
- theItem: ItemReference;
- requestedType: FlavorType;
- dataHandle: Handle): OSErr;
-
- implementation
-
- function ReadTextFile (pFileSpec: FSSpecPtr;
- hWE: WEHandle): OSErr;
- var
- dataForkRefNum, resForkRefNum: Integer;
- hText: Handle;
- hStyleScrap: StScrpHandle;
- fileSize: LongInt;
-
- procedure CleanUp;
- var
- err: OSErr;
- begin
- if (hText <> nil) then
- begin
- DisposeHandle(hText);
- hText := nil;
- end;
- if (dataForkRefNum <> 0) then
- begin
- err := FSClose(dataForkRefNum);
- dataForkRefNum := 0;
- end;
- if (resForkRefNum <> 0) then
- begin
- CloseResFile(resForkRefNum);
- resForkRefNum := 0;
- end;
- end; { CleanUp }
-
- procedure CheckErr (err: OSErr);
- begin
- if (err <> noErr) then
- begin
- ReadTextFile := err;
- CleanUp;
- Exit(ReadTextFile);
- end;
- end; { CheckErr }
-
- begin
- ReadTextFile := noErr;
- dataForkRefNum := 0;
- resForkRefNum := 0;
- hText := nil;
-
- { open the data fork with read-only permission }
- CheckErr(FSpOpenDF(pFileSpec^, fsRdPerm, dataForkRefNum));
-
- { get data fork size }
- CheckErr(GetEOF(dataForkRefNum, fileSize));
-
- { try to allocate a handle that large; use temporary memory if available }
- CheckErr(NewHandleTemp(fileSize, hText));
-
- { read in the text }
- CheckErr(FSRead(dataForkRefNum, fileSize, hText^));
-
- { install the text in the WE instance }
- CheckErr(WEUseText(hText, hWE));
- hText := nil;
-
- { see if the file has a resource fork }
- resForkRefNum := FSpOpenResFile(pFileSpec^, fsRdPerm);
- if (resForkRefNum <> -1) then
- begin
-
- { look for a style scrap resource (get the first one; the resource ID doesn't matter) }
- hStyleScrap := StScrpHandle(Get1IndResource(kTypeStyles, 1));
-
- { if there's a style scrap, apply it to the text }
- if (hStyleScrap <> nil) then
- begin
- WESetSelection(0, maxLongInt, hWE);
- CheckErr(WEUseStyleScrap(hStyleScrap, hWE));
- WESetSelection(0, 0, hWE);
- end;
- end;
-
- { clean up and exit }
- CleanUp;
-
- end; { ReadTextFile }
-
- function WriteTextFile (pFileSpec: FSSpecPtr;
- hWE: WEHandle): OSErr;
- var
- dataForkRefNum, resForkRefNum: Integer;
- hText, hStyles: Handle;
- fileInfo: FInfo;
- fileSize: LongInt;
- replacing: Boolean;
- err: OSErr;
-
- procedure CleanUp;
- var
- err: OSErr;
- begin
-
- if (dataForkRefNum <> 0) then
- begin
- err := FSClose(dataForkRefNum);
- dataForkRefNum := 0;
- end;
-
- if (hStyles <> nil) then
- begin
- ReleaseResource(hStyles);
- hStyles := nil;
- end;
-
- if (resForkRefNum <> 0) then
- begin
- CloseResFile(resForkRefNum);
- resForkRefNum := 0;
- end;
-
- end; { CleanUp }
-
- procedure CheckErr (err: OSErr);
- begin
- if (err <> noErr) then
- begin
- WriteTextFile := err;
- ErrorAlert(err);
- CleanUp;
- Exit(WriteTextFile);
- end;
- end; { CheckErr }
-
- begin
- WriteTextFile := noErr;
- dataForkRefNum := 0;
- resForkRefNum := 0;
- hStyles := nil;
-
- { are we replacing an existing file? }
- err := FSpGetFInfo(pFileSpec^, fileInfo);
- if (err = noErr) then
- replacing := true
- else if (err = fnfErr) then
- replacing := false
- else
- CheckErr(err);
-
- { delete existing file, if any }
- if (replacing) then
- CheckErr(FSpDelete(pFileSpec^));
-
- { create a new file }
- FSpCreateResFile(pFileSpec^, kAppSignature, kTypeText, 0);
- CheckErr(ResError);
-
- { if replacing an old file, copy the old file information }
- if (replacing) then
- CheckErr(FSpSetFInfo(pFileSpec^, fileInfo));
-
- { open the data fork for writing }
- CheckErr(FSpOpenDF(pFileSpec^, fsRdWrPerm, dataForkRefNum));
-
- { get the text handle from the WE instance }
- { WEGetText returns the original handle, not a copy, so don't dispose of it! }
- hText := WEGetText(hWE);
- fileSize := GetHandleSize(hText);
-
- { write the text }
- CheckErr(FSWrite(dataForkRefNum, fileSize, hText^));
-
- { open the resource file for writing }
- resForkRefNum := FSpOpenResFile(pFileSpec^, fsRdWrPerm);
- CheckErr(ResError);
-
- { allocate a temporary handle to hold the style scrap }
- CheckErr(NewHandleTemp(0, hStyles));
-
- { create a style scrap describing the styles of the whole text }
- CheckErr(WECopyRange(0, maxLongInt, nil, StScrpHandle(hStyles), nil, hWE));
-
- { make the style scrap handle a resource handle }
- AddResource(hStyles, kTypeStyles, 128, '');
- CheckErr(ResError);
-
- { write the style scrap to the resource file }
- WriteResource(hStyles);
- CheckErr(ResError);
-
- { "clean" this document by resetting the WE instance modification count }
- { (this clears the undo buffer as well, so that undoing an editing action after saving }
- { doesn't set the modification count to a negative value) }
- WEResetModCount(hWE);
-
- { clean up }
- CleanUp;
-
- end; { WriteTextFile }
-
- function TranslateDrag (theDrag: DragReference;
- theItem: ItemReference;
- requestedType: FlavorType;
- dataHandle: Handle): OSErr;
-
- { this simple routine is meant to give an idea of how the drag translation hook ('xdrg') }
- { is supposed to work -- in the real world I should probably handle styled text files, }
- { PICT files and maybe other fancier file types here: }
- { that is left as an exercise for the reader }
-
- var
- numFlavors: Integer;
- theType: FlavorType;
- hfs: HFSFlavor;
- refNum: Integer;
- dataSize: Size;
- err: OSErr;
-
- procedure CleanUp;
- begin
- if (refNum <> 0) then
- begin
- if (FSClose(refNum) <> noErr) then
- ;
- refNum := 0;
- end;
- end; { CleanUp }
-
- procedure CheckErr (err: OSErr);
- begin
- if (err <> noErr) then
- begin
- TranslateDrag := err;
- CleanUp;
- Exit(TranslateDrag);
- end;
- end; { CheckErr }
-
- begin
- TranslateDrag := badDragFlavorErr; { assume failure }
- refNum := 0;
-
- { we'll try to translate HFS objects to TEXT, so make sure that is the requested type }
- if (requestedType <> kTypeText) then
- Exit(TranslateDrag);
-
- { see if this drag item is a TEXT file }
- dataSize := SizeOf(hfs);
- if (CountDragItemFlavors(theDrag, theItem, numFlavors) = noErr) then
- if (numFlavors = 1) then
- if (GetFlavorType(theDrag, theItem, 1, theType) = noErr) then
- if (theType = flavorTypeHFS) then
- if (GetFlavorData(theDrag, theItem, theType, @hfs, dataSize, 0) = noErr) then
- if (hfs.fileType = kTypeText) then
- begin
- TranslateDrag := noErr; { assume success }
-
- { if dataHandle is NIL, we're finished }
- if (dataHandle = nil) then
- Exit(TranslateDrag);
-
- { open the file for reading }
- CheckErr(FSpOpenDF(hfs.fileSpec, fsRdPerm, refNum));
-
- { get file size }
- CheckErr(GetEOF(refNum, dataSize));
-
- { resize the data handle }
- SetHandleSize(dataHandle, dataSize);
- CheckErr(MemError);
-
- { read the file }
- CheckErr(FSRead(refNum, dataSize, dataHandle^));
- end;
-
- { clean up }
- CleanUp;
-
- end; { TranslateDrag }
-
- end.